package com.tangk.tool.util;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;

public class ReflectUtils {
	private static Logger logger = Logger.getLogger("ReflectUtils");
	@SuppressWarnings("unchecked")
	public static <T> T invokeMehod(Object obj,String methodName,T defaultValue){
		
		try {
			if(methodName == null || methodName == "") throw new RuntimeException();
			return (T) obj.getClass().getMethod(methodName).invoke(obj);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e) {
			logger.warning(e.getMessage());
		}
		
		return defaultValue;
	}
	public static Object newInstance(String objClassName) {
		try {
			Class<?> cls = Class.forName(objClassName);
			try {
				return cls.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
